<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Owning bus names: GIO Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="GIO Reference Manual">
<link rel="up" href="ch35.html" title="Migrating to GDBus">
<link rel="prev" href="ch35s02.html" title="API comparison">
<link rel="next" href="ch35s04.html" title="Creating proxies for well-known names">
<meta name="generator" content="GTK-Doc V1.26.1 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="ch35.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="ch35s02.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="ch35s04.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id-1.5.5.4"></a>Owning bus names</h2></div></div></div>
<p>
      Using dbus-glib, you typically call RequestName manually
      to own a name, like in the following excerpt:
      </p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45</pre></td>
        <td class="listing_code"><pre class="programlisting">error <span class="gtkdoc opt">=</span> NULL<span class="gtkdoc opt">;</span>
res <span class="gtkdoc opt">=</span> <span class="function">dbus_g_proxy_call</span> <span class="gtkdoc opt">(</span>system_bus_proxy<span class="gtkdoc opt">,</span>
                         <span class="string">&quot;RequestName&quot;</span><span class="gtkdoc opt">,</span>
                         <span class="gtkdoc opt">&amp;</span>error<span class="gtkdoc opt">,</span>
                         G_TYPE_STRING<span class="gtkdoc opt">,</span> NAME_TO_CLAIM<span class="gtkdoc opt">,</span>
                         G_TYPE_UINT<span class="gtkdoc opt">,</span>   DBUS_NAME_FLAG_ALLOW_REPLACEMENT<span class="gtkdoc opt">,</span>
                         G_TYPE_INVALID<span class="gtkdoc opt">,</span>
                         G_TYPE_UINT<span class="gtkdoc opt">,   &amp;</span>result<span class="gtkdoc opt">,</span>
                         G_TYPE_INVALID<span class="gtkdoc opt">);</span>
<span class="keyword">if</span> <span class="gtkdoc opt">(!</span>res<span class="gtkdoc opt">)</span>
  <span class="gtkdoc opt">{</span>
    <span class="keyword">if</span> <span class="gtkdoc opt">(</span>error <span class="gtkdoc opt">!=</span> NULL<span class="gtkdoc opt">)</span>
      <span class="gtkdoc opt">{</span>
        <span class="function"><a href="../glib-Message-Logging.html#g-warning">g_warning</a></span> <span class="gtkdoc opt">(</span><span class="string">&quot;Failed to acquire %s: %s&quot;</span><span class="gtkdoc opt">,</span>
                   NAME_TO_CLAIM<span class="gtkdoc opt">,</span> error<span class="gtkdoc opt">-&gt;</span>message<span class="gtkdoc opt">);</span>
        <span class="function"><a href="../glib-Error-Reporting.html#g-error-free">g_error_free</a></span> <span class="gtkdoc opt">(</span>error<span class="gtkdoc opt">);</span>
      <span class="gtkdoc opt">}</span>
    <span class="keyword">else</span>
      <span class="gtkdoc opt">{</span>
        <span class="function"><a href="../glib-Message-Logging.html#g-warning">g_warning</a></span> <span class="gtkdoc opt">(</span><span class="string">&quot;Failed to acquire %s&quot;</span><span class="gtkdoc opt">,</span> NAME_TO_CLAIM<span class="gtkdoc opt">);</span>
      <span class="gtkdoc opt">}</span>
    <span class="keyword">goto</span> out<span class="gtkdoc opt">;</span>
  <span class="gtkdoc opt">}</span>

<span class="keyword">if</span> <span class="gtkdoc opt">(</span>result <span class="gtkdoc opt">!=</span> DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER<span class="gtkdoc opt">)</span>
  <span class="gtkdoc opt">{</span>
    <span class="keyword">if</span> <span class="gtkdoc opt">(</span>error <span class="gtkdoc opt">!=</span> NULL<span class="gtkdoc opt">)</span>
      <span class="gtkdoc opt">{</span>
        <span class="function"><a href="../glib-Message-Logging.html#g-warning">g_warning</a></span> <span class="gtkdoc opt">(</span><span class="string">&quot;Failed to acquire %s: %s&quot;</span><span class="gtkdoc opt">,</span>
                   NAME_TO_CLAIM<span class="gtkdoc opt">,</span> error<span class="gtkdoc opt">-&gt;</span>message<span class="gtkdoc opt">);</span>
        <span class="function"><a href="../glib-Error-Reporting.html#g-error-free">g_error_free</a></span> <span class="gtkdoc opt">(</span>error<span class="gtkdoc opt">);</span>
      <span class="gtkdoc opt">}</span>
    <span class="keyword">else</span>
      <span class="gtkdoc opt">{</span>
        <span class="function"><a href="../glib-Message-Logging.html#g-warning">g_warning</a></span> <span class="gtkdoc opt">(</span><span class="string">&quot;Failed to acquire %s&quot;</span><span class="gtkdoc opt">,</span> NAME_TO_CLAIM<span class="gtkdoc opt">);</span>
      <span class="gtkdoc opt">}</span>
    <span class="function">exit</span> <span class="gtkdoc opt">(</span><span class="number">1</span><span class="gtkdoc opt">);</span>
  <span class="gtkdoc opt">}</span>

<span class="function">dbus_g_proxy_add_signal</span> <span class="gtkdoc opt">(</span>system_bus_proxy<span class="gtkdoc opt">,</span> <span class="string">&quot;NameLost&quot;</span><span class="gtkdoc opt">,</span>
                         G_TYPE_STRING<span class="gtkdoc opt">,</span> G_TYPE_INVALID<span class="gtkdoc opt">);</span>
<span class="function">dbus_g_proxy_connect_signal</span> <span class="gtkdoc opt">(</span>system_bus_proxy<span class="gtkdoc opt">,</span> <span class="string">&quot;NameLost&quot;</span><span class="gtkdoc opt">,</span>
                             <span class="function"><a href="../gobject-Closures.html#G-CALLBACK:CAPS">G_CALLBACK</a></span> <span class="gtkdoc opt">(</span>on_name_lost<span class="gtkdoc opt">),</span> NULL<span class="gtkdoc opt">,</span> NULL<span class="gtkdoc opt">);</span>

<span class="comment">/* further setup ... */</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
    </p>
<p>
    While you can do things this way with GDBus too, using
    <a class="link" href="GDBusProxy.html#g-dbus-proxy-call-sync" title="g_dbus_proxy_call_sync ()"><code class="function">g_dbus_proxy_call_sync()</code></a>, it is much nicer to use the high-level API
    for this:
    </p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="gtkdoc kwb">static void</span>
<span class="function">on_name_acquired</span> <span class="gtkdoc opt">(</span>GDBusConnection <span class="gtkdoc opt">*</span>connection<span class="gtkdoc opt">,</span>
                  <span class="gtkdoc kwb">const</span> gchar     <span class="gtkdoc opt">*</span>name<span class="gtkdoc opt">,</span>
                  gpointer         user_data<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
  <span class="comment">/* further setup ... */</span>
<span class="gtkdoc opt">}</span>

<span class="comment">/* ... */</span>

  owner_id <span class="gtkdoc opt">=</span> <span class="function"><a href="gio-Owning-Bus-Names.html#g-bus-own-name">g_bus_own_name</a></span> <span class="gtkdoc opt">(</span>G_BUS_TYPE_SYSTEM<span class="gtkdoc opt">,</span>
                             NAME_TO_CLAIM<span class="gtkdoc opt">,</span>
                             G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT<span class="gtkdoc opt">,</span>
                             on_bus_acquired<span class="gtkdoc opt">,</span>
                             on_name_acquired<span class="gtkdoc opt">,</span>
                             on_name_lost<span class="gtkdoc opt">,</span>
                             NULL<span class="gtkdoc opt">,</span>
                             NULL<span class="gtkdoc opt">);</span>

  <span class="function"><a href="../glib-The-Main-Event-Loop.html#g-main-loop-run">g_main_loop_run</a></span> <span class="gtkdoc opt">(</span>loop<span class="gtkdoc opt">);</span>

  <span class="function"><a href="gio-Owning-Bus-Names.html#g-bus-unown-name">g_bus_unown_name</a></span> <span class="gtkdoc opt">(</span>owner_id<span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
    Note that <a class="link" href="gio-Owning-Bus-Names.html#g-bus-own-name" title="g_bus_own_name ()"><code class="function">g_bus_own_name()</code></a> works asynchronously and requires
    you to enter your mainloop to await the <code class="function">on_name_aquired()</code>
    callback. Also note that in order to avoid race conditions (e.g.
    when your service is activated by a method call), you have to export
    your manager object <span class="emphasis"><em>before</em></span> acquiring the
    name. The <code class="function">on_bus_acquired()</code> callback is the right place to do
    such preparations.
    </p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.26.1</div>
</body>
</html>